from django.http import HttpResponseBadRequest, HttpResponse, HttpRequest, JsonResponse
import hashlib
from .models import NewsClass, NewsSeo, NewsPower, NewsList, NewsSysteam
from django.db.models import Q
from django.core.paginator import Paginator
import simplejson
import django.utils.timezone as timezone


def newslist(request):
	if request.method == 'POST':
		# request.params = request.GET
		return newslist_add(request)
	# # 添加管理用户
	elif request.method == 'GET':
		# request.params = simplejson.loads(request.body)
		return newslist_list(request)
	# # 更新用户信息
	elif request.method == 'PUT':
		return newslist_edit(request)
	# # 删除管理用户： 逻辑删除
	elif request.method == 'DELETE':
		return newslist_delete(request)

	else:
		return JsonResponse({"state": 0, "msg": "信息传递有问题"})


def newslist_add(request):
	# return HttpResponse('aaaaadsgbsvb')
	info = request.POST
	titles = info['titles']
	conntent = info['conntent']
	created_time = timezone.now()
	updatad_time = timezone.now()
	print(conntent, titles)
	try:
		print("-----" * 5)
		item = NewsList.objects.filter(Q(titles=titles) & Q(conntent=conntent))
		print("//////" * 100)
		print(item, item.query)
		if item:
			return JsonResponse({"state": 0, "msg": info['titles'] + "已存在,不能重复添加"})
		else:
			print("--------" * 100)

			if info['state']:
				state = info['state']
			else:
				state = 1

			print("+++++++" * 100)
			try:
				qs = NewsList.objects.create(titles=info['titles'], classes_id=info['classes_id'], img=info['img'], description=info['description'], conntent=info['conntent'], author=info['author'], tag=info['tag'], clicks=info['clicks'], created_by=info['created_by'], created_time=created_time, updatad_by=info['updatad_by'], updatad_time=updatad_time, state=state, power_id=1)
				# info.save()
				# return JsonResponse('', safe=False)
				return JsonResponse({"state": 1, "msg": qs.titles + "添加成功"})

			except NewsList.DoesNotExist:
				return JsonResponse({"state": 0, "msg": "不能添加"})

	except NewsList.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "信息错误"})


def newslist_list(request):
	data = {}
	# 获取传递过来的page
	try:
		page = request.GET.get("page")
		# 如果没有值则默认给一个值
		if not page:
			page = 1
		# 保证传递过来的值是整形
		page_id = int(page)
		# 查询所有用户
		qs = NewsList.objects.all().values()
		# 创建分页对象  qs 后面的值 是一个页面有多少数据
		paginator = Paginator(qs, 3)
		# 根据当前页码,确定返回的数据
		current_page = paginator.page(page_id)
		# print(current_page)

		data["num"] = 1
		data["msg"] = list(current_page)

		return JsonResponse(data, safe=False)
	except Exception as e:
		return JsonResponse({"state": 0, "msg": "错误传值"})


def newslist_edit(request):
	# info = request.body
	info = simplejson.loads(request.body)
	# print(info['id'], info["newdata"]["username"])
	ids = info["newdata"]["id"]
	newsdata = info["newdata"]
	try:
		customer = NewsList.objects.get(pk=ids)
	except NewsList.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "信息存在,不能重复添加"})

	if 'titles' in newsdata:
		customer.titles = newsdata['titles']
	if 'conntent' in newsdata:
		customer.conntent = newsdata['conntent']
	if 'img' in newsdata:
		customer.imgs = newsdata['img']
	if 'description' in newsdata:
		customer.description = newsdata['description']
	if 'author' in newsdata:
		customer.author = newsdata['author']
	if 'tag' in newsdata:
		customer.tags = newsdata['tag']
	if 'clicks' in newsdata:
		customer.clicks = newsdata['clicks']
	if 'description' in newsdata:
		customer.description = newsdata['description']
	if 'updata_time' in newsdata:
		customer.phone = newsdata['updata_time']
	customer.update_time = timezone.now()
	customer.save()
	return JsonResponse({"state": 1, "msg": "修改成功"})


def newslist_delete(request):
	info = simplejson.loads(request.body)
	if len(info) == 1:
		ids = info['id']
		stateid = 3
	else:
		ids = info['id']
		stateid = info['status']

	try:
		delete_users = NewsList.objects.get(pk=ids)
		# print(delete_user)
	except NewsList.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "用户不存在"})

	if stateid == 1 or stateid == 0:
		delete_users.state = stateid
		if stateid == 1:
			delete_users.delete_time is None
		delete_users.save()
		return JsonResponse({"state": 1, "msg": "修改成功"})
	else:

		if delete_users.delete_time is not None:
			return JsonResponse({"state": 0, "msg": "用户已经删除"})
		else:
			# customer.delete()
			delete_users.state = 0
			# 采用逻辑删除
			delete_users.delete_time = timezone.now()
			delete_users.save()
			return JsonResponse({"state": 1, "msg": "删除成功"})